package com.sixmac.controller;

import com.google.common.base.Strings;
import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.annotation.Validator;
import com.sixmac.core.base.model.Format;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.DateKit;
import com.sixmac.module.Landlord;
import com.sixmac.platform.service.LoginService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by admin on 2018/8/31.
 */
@Controller
@ResponseBody
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Autowired
    LoginService loginService;


    /**
     * @api {post} /login/sendCaptcha 0.1发送验证码
     * @apiName login.sendCaptcha
     * @apiGroup login
     * @apiParam {String} phone 手机号码
     */
    @AuthUser
    @Validator(name = {"phone"}, format = {Format.REG_PHONE})
    @RequestMapping("/sendCaptcha")
    public AjaxResult sendCaptcha(String token,String phone) {
        return loginService.sendCaptcha(token,phone);
    }

    /**
     * @api {post} /login/openId 0.2.1登录
     * @apiName login.openId
     * @apiGroup login
     * @apiParam {String} code 用户code
     * @apiParam {String} headImage 用户头像
     * @apiParam {String} name 用户昵称
     * @apiSuccess {String} user.code 123需要去绑定手机号码
     * @apiSuccess {String} user.id 房东id
     * @apiSuccess {String} user.token token
     * @apiSuccess {String} user.name 房东名称
     * @apiSuccess {String} user.mobile 手机号码
     * @apiSuccess {String} user.sex 性别 1男2女
     * @apiSuccess {String} user.birthday 出生日期
     * @apiSuccess {String} user.region 区域
     * @apiSuccess {String} user.contract 合同照片
     * @apiSuccess {String} user.idCard 身份证号码
     * @apiSuccess {String} user.businessLicense 经营许可证
     * @apiSuccess {String} user.idCardFront 身份证正面
     * @apiSuccess {String} user.idCardReverse 身份证反面
     */
    @RequestMapping("/openId")
    public AjaxResult login(String code, String headImage, String name) {
        return loginService.login(code, headImage, name);
    }


    /**
     * @api {post} /login/bindMobile 0.2.2绑定手机
     * @apiName login.bindMobile
     * @apiGroup login
     * @apiParam {String} token token
     * @apiParam {String} phone 手机号码
     * @apiParam {String} captcha 验证码
     */
    @Validator(name = {"phone", "captcha"}, format = {Format.REG_PHONE, Format.NOT_NULL})
    @RequestMapping("/bindMobile")
    public AjaxResult bindMobile(String token, String phone, String captcha) {
        return loginService.bindMobile(token,phone, captcha);
    }

    /**
     * @api {post} /login/editLandlord 0.3编辑用户（提交个人信息资料）
     * @apiName login.editLandlord
     * @apiGroup login
     * @apiParam {String} token token
     * @apiParam {String} phone 手机号码
     * @apiParam {String} name 房东名称
     * @apiParam {String} sex 性别 1男2女
     * @apiParam {String} birth 出生日期 比如（2018-09-03）
     * @apiParam {String} region 区域
     * @apiParam {String} contract 合同照片
     * @apiParam {String} idCard 身份证号码
     * @apiParam {String} businessLicense 经营许可证
     * @apiParam {String} idCardFront 身份证正面
     * @apiParam {String} idCardReverse 身份证反面
     * @apiParam {String} region areaCode
     */
    @AuthUser
    @RequestMapping("/editLandlord")
    public AjaxResult editLandlord(Landlord landlord1, String name, String phone, Integer sex,
                                   String birthday, String idCard, String idCardFront,
                                   String idCardReverse, String businessLicense, Integer region) {
        Blade.create(Landlord.class).findById(landlord1.getId());

        if (!loginService.checkPhoneOrIdCard(landlord1.getId(),phone, idCard)) {
            return error("身份证号码已经存在");
        }

        Landlord landlord = new Landlord();
        landlord.setId(landlord1.getId());
        landlord.setName(name);
        landlord.setMobile(phone);
        landlord.setSex(sex);
        if (!Strings.isNullOrEmpty(birthday)) {
            landlord.setBirthday(DateKit.parse(birthday, "yyyy-MM-dd"));
        }

        landlord.setIdCard(idCard);
        landlord.setIdCardFront(idCardFront);
        landlord.setIdCardReverse(idCardReverse);
        landlord.setBusinessLicense(businessLicense);
        landlord.setRegion(region);
        Blade.create(Landlord.class).update(landlord);

        return success("");
    }

    @Test
    public void te() {
        System.out.print(DateKit.format(new Date(), "yyyy-MM-dd"));
    }


}
